//初始化树结构（合并初始内容）
var baseUrl = "";
var token = sessionStorage.getItem("token");
VanyAjax.token = token;
VanyAjax.baseUrl = baseUrl;
var setting3 = {
	data: {
		key: {
			name: "roleName"
		}
	}
};
var NodeSetting;
if("undefined" == typeof setting2) {
	setting2 = '';
}
NodeSetting = $.extend({}, setting1, setting2, setting3);
new VanyBaseTree("#treeDemo", baseUrl + "/sys/role/loadRoleInfo?token=" + sessionStorage.token, NodeSetting);

//节点角色人员展示
var RoleList = RoleOrg("rolePerson", "authority/qxgl/json/rolePerson.json");

//刷新表格
//function zTreeOnClick(treeNode) {
//	console.log("刷新权限管理")
//}

//树节点操作
var editTree = new Vue({
	el: '#rMenu',
	methods: {
		//编辑节点
		Edit: function() {
			hideRMenu();
			//获取树节点相关信息
			var treeKey = zTree.getSelectedNodes()[0];
			for(var key in treeKey) {
				treeEdit.ruleForm[key] = treeKey[key];
				if(key == "state") {
					zTree.getSelectedNodes()[0].state == 1 ? treeEdit.ruleForm[key] = 1 : treeEdit.ruleForm[key] = 0
				}
			}
			treeEdit.treeEditVisible = true;
		},
		//删除节点
		del: function() {
			hideRMenu();
			delNode(this);
		},
		//开启
		EnableUse: function() {
			EnableUse(this);
		},
		//禁用
		DisableUse: function() {
			DisableUse(this);
		},
		//批量操作
		AddPeople: function() {
			RoleList.getRolePerson();
		}
	}
});

//新增节点
var addTitles = new Vue({
	el: "#treeTitle",
	methods: {
		addTitle: function() {
			hideRMenu();
			addNodes.treeAddVisible = true;
		},
		handleClose: function(done) {
			this.$confirm('确认关闭？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function() {
				done();
			}).catch(function() {})
		}
	}
});
var addNodes = new Vue({
	el: "#addRole",
	data: function() {
		//自定义验证，验证机构编码
		var checkId = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入角色编码'));
			}
			setTimeout(function() {
				//中文判断
				if(!/^[a-zA-Z0-9_]{0,}$/.test(addNodes.ruleForm.id)) {
					callback(new Error('编码不能为中文字符'));
				} else {
					callback();
				}
			});
		};
		//验证机构号码
		var checkofficeTel = function(rule, value, callback) {
			if(!value) {
				callback();
			}
			setTimeout(function() {
				//数字判断
				if(!/^[0-9]*$/.test(addNodes.ruleForm.officeTel)) {
					callback(new Error('电话只能是数字'));
				} else {
					callback();
				}
			});
		};
		return {
			treeAddVisible: false,
			labelPosition: 'right',
			ruleForm: {
				id: '',
				name: '',
				officeTel: '',
				state: 1
			},
			rules: {
				id: {
					required: true,
					validator: checkId,
					trigger: 'blur'
				},
				name: {
					required: true,
					message: '请输入角色名称',
					trigger: 'blur'
					//				},
					//				officeTel: {
					//					required: false,
					//					validator: checkofficeTel,
					//					trigger: 'blur'
				}
			}
		};
	},
	methods: {
		submitForm: function(formName) {
			var self = this;
			self.$refs[formName].validate(function(valid) {
				if(valid) {
					addNodes.treeAddVisible = false;
					VanyAjax("/sys/role/addRole", {
						id: addNodes.ruleForm.id,
						roleName: addNodes.ruleForm.name,
						isEnable: addNodes.ruleForm.state
					}).done(function(data) {
						var newNode = {
							id: addNodes.ruleForm.id,
							name: addNodes.ruleForm.name
						};
						//判断是否是标题中的新增节点，如果是，则新增根节点
						zTree.addNodes(null, -1, newNode, false);
						self.$message({
							type: 'success',
							message: addNodes.ruleForm.name + '已添加成功!'
						});
						addNodes.ruleForm.id = '';
						addNodes.ruleForm.name = '';
						addNodes.ruleForm.officeTel = '';
					}, false, this);
				} else {
					console.log('error submit!!');
					return false;
				}
			});
		},
		handleClose: function(done) {
			done();
//			this.$confirm('确认关闭？', '提示', {
//				confirmButtonText: '确定',
//				cancelButtonText: '取消',
//				type: 'warning'
//			}).then(function() {
//				done();
//			}).catch(function() {})
		},
		resetForm: function(formName) {
			this.$refs[formName].resetFields();
		} //重置
	}
});

//删除节点
function delNode(self) {
	VanyAjax("/base/org/delete", {
		orgCode: zTree.getSelectedNodes()[0].id
	}).done(function(data) {
		removeTreeNode(self);
		self.$message({
			type: 'success',
			message: '删除成功!'
		});
	}, false, this);
}

function removeTreeNode(self) {
	var nodes = zTree.getSelectedNodes();
	if(nodes && nodes.length > 0) {
		zTree.removeNode(nodes[0]);
	}
}

//树的修改
var treeEdit = new Vue({
	el: "#treeEdit",
	data: function() {
		//验证机构号码
		var checkofficeTel = function(rule, value, callback) {
			if(!value) {
				callback();
			}
			setTimeout(function() {
				//数字判断
				if(!/^[0-9]*$/.test(treeEdit.ruleForm.officeTel)) {
					callback(new Error('电话只能是数字'));
				} else {
					callback();
				}
			});
		};
		return {
			treeEditVisible: false,
			labelPosition: 'right',
			ruleForm: {
				id: '',
				name: '',
				officeTel: '',
				state: 1
			},
			rules: {
				name: {
					required: true,
					message: '请输入机构名称',
					trigger: 'blur'
				},
				officeTel: {
					required: false,
					validator: checkofficeTel,
					trigger: 'blur'
				}
			}
		};
	},
	methods: {
		submitForm: function(formName) {
			var self = this;
			self.$refs[formName].validate(function(valid) {
				if(valid) {
					//判断是否有父级节点
					if(zTree.getSelectedNodes()[0].getParentNode() == null) {
						parentOrgCodeNodeId = null;
					} else {
						parentOrgCodeNodeId = zTree.getSelectedNodes()[0].getParentNode().id;
					}
					treeEdit.treeEditVisible = false;
					VanyAjax("/base/org/update", {
						orgCode: treeEdit.ruleForm.id,
						parentOrgCode: parentOrgCodeNodeId,
						orgName: treeEdit.ruleForm.name,
						officeTel: treeEdit.ruleForm.officeTel
					}).done(function(data) {
						zTree.getSelectedNodes()[0].name = treeEdit.ruleForm.name; //获取名称
						zTree.getSelectedNodes()[0].state = treeEdit.ruleForm.state; //1启用？0禁用
						zTree.updateNode(zTree.getSelectedNodes()[0]);
						self.$message({
							type: 'success',
							message: '已成功修改!'
						});
					}, false, this);
				} else {
					console.log('error submit!!');
					return false;
				}
			});
		},
		handleClose: function(done) {
			done();
//			this.$confirm('确认关闭？', '提示', {
//				confirmButtonText: '确定',
//				cancelButtonText: '取消',
//				type: 'warning'
//			}).then(function() {
//				done();
//			}).catch(function() {})
		},
		resetForm: function(formName) {
			this.$refs[formName].resetFields();
		} //重置
	}
});

//穿梭框
new Vue().$mount('#transfertabs');
transfer = new VanyTransfer("#transfer", "authority/qxgl/json/transfer.json");
// 42是选项卡高度，15是padding高度，2是边框高度
//var _treeHeight = $("#move_left").height() - 42 - 30 - 2;
transfer.setTreeHeight(645);